package snu.bi.MSAgent;

import java.util.Observable;
import java.util.Observer;
import java.util.TimeZone;

import snu.bi.MSAgent.ActRecog.ActionService;
import snu.bi.MSAgent.DB.MSADBHandler;
import snu.bi.MSAgent.Estimator.WeightEstimator;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class ExerActivity extends Activity implements Observer{
	Button ok;
	TextView exer_time_value_run, exer_time_value_walk, cal_in_text, cal_out_text, aday_tv, amonth_tv, Astate_tv;
	
	private static ExerActivity sInstance = null;

	public static ExerActivity getInstance(){
		return ExerActivity.sInstance;
	}

    public void setObservable(ActionService as){
    	as.setObserver(this);
    }
	
	BroadcastReceiver rec = new BroadcastReceiver(){
		@Override
		public void onReceive(Context context, Intent intent){
			Log.i("test", "Receive?");
//			if(intent.getAction().equals("snu.bi.MSAgent.ActRecog.ActionService.MY_ACTION")){
				Astate_tv.setText(intent.getStringExtra("CURR_ACT"));
				Log.i("test", "Receive");
//			}
		}
	};
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.recom_exer_lay);	
		
		TimeZone timezone = TimeZone.getTimeZone("Etc/GMT-9");
        TimeZone.setDefault(timezone);
		
		exer_time_value_run = (TextView)findViewById(R.id.recom_exer_time_value_run);
		exer_time_value_walk = (TextView)findViewById(R.id.recom_exer_time_value_walk);
		cal_in_text = (TextView)findViewById(R.id.recom_exer_intake_value);
		cal_out_text = (TextView)findViewById(R.id.recom_exer_cons_value);
		aday_tv = (TextView)findViewById(R.id.aday_value);
		amonth_tv = (TextView)findViewById(R.id.amonth_value);
		Astate_tv = (TextView)findViewById(R.id.Action_state_value);
		
		ok = (Button)findViewById(R.id.recom_exer_ok);		

		
//		rec = new ActReceiver();
//=======
		this.display(0);

		ExerActivity.sInstance = this;
//>>>>>>> .r22
	}
	public void onResume(){
		super.onResume();
		IntentFilter inf = new IntentFilter();
		inf.addAction("snu.bi.MSAgent.ActRecog.ActionService.intetn.action.MY_ACTION");
		registerReceiver(rec, inf);
		Log.i("test", "Registered?");
	}
	public void onPause(){
		super.onPause();
		unregisterReceiver(rec);
		Log.i("test", "Unregistered?");
	}
	
	public void returnback(View view) {
		finish();
	}
	private void calculate_exer_time(double in, double out, double MET) {
		double min = 0.0;
		
		MSADBHandler tempdb = MSADBHandler.open(this.getApplicationContext());
		WeightEstimator we = new WeightEstimator(tempdb);
		
		double to_goal = in-out+we.cal_to_fitness();
		
		if(in<out) min = 0.0;
		else {
			min = to_goal/(we.cals_per_min(MET));
			min = Math.round(min*100)/100;
		}
		tempdb.close();
		exer_time_value_walk.setText(Double.toString(min)+"min");
	}
	public void Action_state_display(int state) {

		if(state == -1) Astate_tv.setText("Run");
		else if(state == 1) Astate_tv.setText("Walk");
		else if(state == 0) Astate_tv.setTag("Stay");

		if(state == ActionService.ACTION_RUN) Astate_tv.setText("Run");
		else if(state == ActionService.ACTION_WALK) Astate_tv.setText("walk");
		else if(state == ActionService.ACTION_STAY) Astate_tv.setText("Stay");
		else Astate_tv.setText("etc");
	}
	private void aday_weight_display(double kg) {		
		aday_tv.setText(Double.toString(Math.round(kg*100)/100)+" kg");
	}
	private void amonth_weight_display(double kg) {
		amonth_tv.setText(Double.toString(Math.round(kg*100)/100)+" kg");
	}
	public void display(int Action_type) {
		double in, out, aday, amonth;
		MSADBHandler tempdb = MSADBHandler.open(this.getApplicationContext());
		WeightEstimator we = new WeightEstimator(tempdb);
		
		in = we.calculateIntakeCalories(1);
		out = we.burnedUpCalories(1);
		aday = we.estimateWeight(1);
		amonth = we.estimateWeight(30);
		
		tempdb.close();
		
		cal_in_text.setText(Double.toString(Math.round(in*100)/100)+" kcal");
		cal_out_text.setText(Double.toString(Math.round(out*100)/100)+" kcal");
		
		calculate_exer_time(in, out, 6);
		calculate_exer_time(in, out, 1.75);	
		
		aday_weight_display(aday);
		amonth_weight_display(amonth);
		Action_state_display(Action_type);
	}
	@Override
	public void update(Observable observable, Object data) {
		// TODO Auto-generated method stub
		cal_in_text.setText((String)data);
		if(((String)data).equals("Run")){
			this.display(ActionService.ACTION_RUN);
		}else if(((String)data).equals("Walk")){
			this.display(ActionService.ACTION_WALK);
		}else if(((String)data).equals("Stay")){
			this.display(ActionService.ACTION_STAY);
		}		
	}	
}
